home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gr564s.zip / SRC / RCSKEEP.C < prev    next >
C/C++ Source or Header  |  1992-09-05  |  10KB  |  426 lines

  1. /*
  2.  *                     RCS keyword extraction
  3.  */
  4. /*****************************************************************************
  5.  *                       main routine: getoldkeys()
  6.  *                       Testprogram: define KEEPTEST
  7.  *****************************************************************************
  8.  */
  9.  
  10. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  11.    Copyright 1990, 1991, 1992 by Paul Eggert
  12.    Distributed under license by the Free Software Foundation, Inc.
  13.  
  14. This file is part of RCS.
  15.  
  16. RCS is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2, or (at your option)
  19. any later version.
  20.  
  21. RCS is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with RCS; see the file COPYING.  If not, write to
  28. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  
  30. Report problems and direct all questions to:
  31.  
  32.     rcs-bugs@cs.purdue.edu
  33.  
  34. */
  35.  
  36.  
  37.  
  38. /* $Log: rcskeep.c,v $
  39.  * Revision 5.5  1992/07/28  16:12:44  eggert
  40.  * Statement macro names now end in _.
  41.  *
  42.  * Revision 5.4  1991/08/19  03:13:55  eggert
  43.  * Tune.
  44.  *
  45.  * Revision 5.3  1991/04/21  11:58:25  eggert
  46.  * Shorten names to keep them distinct on shortname hosts.
  47.  *
  48.  * Revision 5.2  1990/10/04  06:30:20  eggert
  49.  * Parse time zone offsets; future RCS versions may output them.
  50.  *
  51.  * Revision 5.1  1990/09/20  02:38:56  eggert
  52.  * ci -k now checks dates more thoroughly.
  53.  *
  54.  * Revision 5.0  1990/08/22  08:12:53  eggert
  55.  * Retrieve old log message if there is one.
  56.  * Don't require final newline.
  57.  * Remove compile-time limits; use malloc instead.  Tune.
  58.  * Permit dates past 1999/12/31.  Ansify and Posixate.
  59.  *
  60.  * Revision 4.6  89/05/01  15:12:56  narten
  61.  * changed copyright header to reflect current distribution rules
  62.  * 
  63.  * Revision 4.5  88/08/09  19:13:03  eggert
  64.  * Remove lint and speed up by making FILE *fp local, not global.
  65.  * 
  66.  * Revision 4.4  87/12/18  11:44:21  narten
  67.  * more lint cleanups (Guy Harris)
  68.  * 
  69.  * Revision 4.3  87/10/18  10:35:50  narten
  70.  * Updating version numbers. Changes relative to 1.1 actually relative
  71.  * to 4.1
  72.  * 
  73.  * Revision 1.3  87/09/24  14:00:00  narten
  74.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  75.  * warnings)
  76.  * 
  77.  * Revision 1.2  87/03/27  14:22:29  jenkins
  78.  * Port to suns
  79.  * 
  80.  * Revision 4.1  83/05/10  16:26:44  wft
  81.  * Added new markers Id and RCSfile; extraction added.
  82.  * Marker matching with trymatch().
  83.  * 
  84.  * Revision 3.2  82/12/24  12:08:26  wft
  85.  * added missing #endif.
  86.  *
  87.  * Revision 3.1  82/12/04  13:22:41  wft
  88.  * Initial revision.
  89.  *
  90.  */
  91.  
  92. /*
  93. #define KEEPTEST
  94. */
  95. /* Testprogram; prints out the keyword values found. */
  96.  
  97. #include  "rcsbase.h"
  98.  
  99. libId(keepId, "$Id: rcskeep.c,v 5.5 1992/07/28 16:12:44 eggert Exp $")
  100.  
  101. static int checknum P((char const*,int));
  102. static int getval P((RILE*,struct buf*,int));
  103. static int get0val P((int,RILE*,struct buf*,int));
  104. static int keepdate P((RILE*));
  105. static int keepid P((int,RILE*,struct buf*));
  106. static int keeprev P((RILE*));
  107.  
  108. int prevkeys;
  109. struct buf prevauthor, prevdate, prevrev, prevstate;
  110.  
  111.     int
  112. getoldkeys(fp)
  113.     register RILE *fp;
  114. /* Function: Tries to read keyword values for author, date,
  115.  * revision number, and state out of the file fp.
  116.  * If FNAME is nonnull, it is opened and closed instead of using FP.
  117.  * The results are placed into
  118.  * prevauthor, prevdate, prevrev, prevstate.
  119.  * Aborts immediately if it finds an error and returns false.
  120.  * If it returns true, it doesn't mean that any of the
  121.  * values were found; instead, check to see whether the corresponding arrays
  122.  * contain the empty string.
  123.  */
  124. {
  125.     register int c;
  126.     char keyword[keylength+1];
  127.     register char * tp;
  128.     int needs_closing;
  129.  
  130.     if (prevkeys)
  131.     return true;
  132.  
  133.     needs_closing = false;
  134.     if (!fp) {
  135.     if (!(fp = Iopen(workname, FOPEN_R_WORK, (struct stat*)0))) {
  136.         eerror(workname);
  137.         return false;
  138.     }
  139.     needs_closing = true;
  140.     }
  141.  
  142.     /* initialize to empty */
  143.     bufscpy(&prevauthor, "");
  144.     bufscpy(&prevdate, "");
  145.     bufscpy(&prevrev, "");
  146.     bufscpy(&prevstate, "");
  147.  
  148.     c = '\0'; /* anything but KDELIM */
  149.     for (;;) {
  150.         if ( c==KDELIM) {
  151.         do {
  152.         /* try to get keyword */
  153.         tp = keyword;
  154.         for (;;) {
  155.             Igeteof_(fp, c, goto ok;)
  156.             switch (c) {
  157.             default:
  158.                 if (keyword+keylength <= tp)
  159.                 break;
  160.                 *tp++ = c;
  161.                 continue;
  162.  
  163.             case '\n': case KDELIM: case VDELIM:
  164.                 break;
  165.             }
  166.             break;
  167.         }
  168.         } while (c==KDELIM);
  169.             if (c!=VDELIM) continue;
  170.         *tp = c;
  171.         Igeteof_(fp, c, break;)
  172.         switch (c) {
  173.         case ' ': case '\t': break;
  174.         default: continue;
  175.         }
  176.  
  177.         switch (trymatch(keyword)) {
  178.             case Author:
  179.         if (!keepid(0, fp, &prevauthor))
  180.             return false;
  181.         c = 0;
  182.                 break;
  183.             case Date:
  184.         if (!(c = keepdate(fp)))
  185.             return false;
  186.                 break;
  187.             case Header:
  188.             case Id:
  189.         if (!(
  190.               getval(fp, (struct buf*)nil, false) &&
  191.               keeprev(fp) &&
  192.               (c = keepdate(fp)) &&
  193.               keepid(c, fp, &prevauthor) &&
  194.               keepid(0, fp, &prevstate)
  195.         ))
  196.             return false;
  197.         /* Skip either ``who'' (new form) or ``Locker: who'' (old).  */
  198.         if (getval(fp, (struct buf*)nil, true) &&
  199.             getval(fp, (struct buf*)nil, true))
  200.             c = 0;
  201.         else if (nerror)
  202.             return false;
  203.         else
  204.             c = KDELIM;
  205.         break;
  206.             case Locker:
  207.             case Log:
  208.             case RCSfile:
  209.             case Source:
  210.         if (!getval(fp, (struct buf*)nil, false))
  211.             return false;
  212.         c = 0;
  213.                 break;
  214.             case Revision:
  215.         if (!keeprev(fp))
  216.             return false;
  217.         c = 0;
  218.                 break;
  219.             case State:
  220.         if (!keepid(0, fp, &prevstate))
  221.             return false;
  222.         c = 0;
  223.                 break;
  224.             default:
  225.                continue;
  226.             }
  227.         if (!c)
  228.         Igeteof_(fp, c, c=0;)
  229.         if (c != KDELIM) {
  230.         error("closing %c missing on keyword", KDELIM);
  231.         return false;
  232.         }
  233.         if (*prevauthor.string && *prevdate.string && *prevrev.string && *prevstate.string) {
  234.                 break;
  235.            }
  236.         }
  237.     Igeteof_(fp, c, break;)
  238.     }
  239.  
  240.  ok:
  241.     if (needs_closing)
  242.     Ifclose(fp);
  243.     else
  244.     Irewind(fp);
  245.     prevkeys = true;
  246.     return true;
  247. }
  248.  
  249.     static int
  250. badly_terminated()
  251. {
  252.     error("badly terminated keyword value");
  253.     return false;
  254. }
  255.  
  256.     static int
  257. getval(fp, target, optional)
  258.     register RILE *fp;
  259.     struct buf *target;
  260.     int optional;
  261. /* Reads a keyword value from FP into TARGET.
  262.  * Returns true if one is found, false otherwise.
  263.  * Does not modify target if it is nil.
  264.  * Do not report an error if OPTIONAL is set and KDELIM is found instead.
  265.  */
  266. {
  267.     int c;
  268.     Igeteof_(fp, c, return badly_terminated();)
  269.     return get0val(c, fp, target, optional);
  270. }
  271.  
  272.     static int
  273. get0val(c, fp, target, optional)
  274.     register int c;
  275.     register RILE *fp;
  276.     struct buf *target;
  277.     int optional;
  278. /* Reads a keyword value from C+FP into TARGET, perhaps OPTIONALly.
  279.  * Same as getval, except C is the lookahead character.
  280.  */
  281. {   register char * tp;
  282.     char const *tlim;
  283.     register int got1;
  284.  
  285.     if (target) {
  286.     bufalloc(target, 1);
  287.     tp = target->string;
  288.     tlim = tp + target->size;
  289.     } else
  290.     tlim = tp = 0;
  291.     got1 = false;
  292.     for (;;) {
  293.     switch (c) {
  294.         default:
  295.         got1 = true;
  296.         if (tp) {
  297.             *tp++ = c;
  298.             if (tlim <= tp)
  299.             tp = bufenlarge(target, &tlim);
  300.         }
  301.         break;
  302.  
  303.         case ' ':
  304.         case '\t':
  305.         if (tp) {
  306.             *tp = 0;
  307. #            ifdef KEEPTEST
  308.             VOID printf("getval: %s\n", target);
  309. #            endif
  310.         }
  311.         if (!got1)
  312.             error("too much white space in keyword value");
  313.         return got1;
  314.  
  315.         case KDELIM:
  316.         if (!got1 && optional)
  317.             return false;
  318.         /* fall into */
  319.         case '\n':
  320.         case 0:
  321.         return badly_terminated();
  322.     }
  323.     Igeteof_(fp, c, return badly_terminated();)
  324.     }
  325. }
  326.  
  327.  
  328.     static int
  329. keepdate(fp)
  330.     RILE *fp;
  331. /* Function: reads a date prevdate; checks format
  332.  * Return 0 on error, lookahead character otherwise.
  333.  */
  334. {
  335.     struct buf prevday, prevtime, prevzone;
  336.     register char const *p;
  337.     register int c;
  338.  
  339.     c = 0;
  340.     bufautobegin(&prevday);
  341.     if (getval(fp,&prevday,false)) {
  342.     bufautobegin(&prevtime);
  343.     if (getval(fp,&prevtime,false)) {
  344.         bufautobegin(&prevzone);
  345.         bufscpy(&prevzone, "");
  346.         Igeteof_(fp, c, c=0;)
  347.         if (c=='-' || c=='+')
  348.         if (!get0val(c,fp,&prevzone,false))
  349.             c = 0;
  350.         else
  351.             Igeteof_(fp, c, c=0;)
  352.         if (c) {
  353.         p = prevday.string;
  354.         bufalloc(&prevdate, strlen(p) + strlen(prevtime.string) + strlen(prevzone.string) + 5);
  355.         VOID sprintf(prevdate.string, "%s%s %s %s",
  356.             /* Parse dates put out by old versions of RCS.  */
  357.             isdigit(p[0]) && isdigit(p[1]) && p[2]=='/'  ?  "19"  :  "",
  358.             p, prevtime.string, prevzone.string
  359.         );
  360.         }
  361.         bufautoend(&prevzone);
  362.     }
  363.     bufautoend(&prevtime);
  364.     }
  365.     bufautoend(&prevday);
  366.     return c;
  367. }
  368.  
  369.     static int
  370. keepid(c, fp, b)
  371.     int c;
  372.     RILE *fp;
  373.     struct buf *b;
  374. /* Get previous identifier from C+FP into B.  */
  375. {
  376.     if (!c)
  377.         Igeteof_(fp, c, return false;)
  378.     if (!get0val(c, fp, b, false))
  379.         return false;
  380.     checksid(b->string);
  381.     return true;
  382. }
  383.  
  384.     static int
  385. keeprev(fp)
  386.     RILE *fp;
  387. /* Get previous revision from FP into prevrev.  */
  388. {
  389.     return getval(fp,&prevrev,false) && checknum(prevrev.string,-1);
  390. }
  391.  
  392.  
  393.     static int
  394. checknum(sp,fields)
  395.     register char const *sp;
  396.     int fields;
  397. {    register int dotcount;
  398.      dotcount=0;
  399.      while(*sp) {
  400.         if (*sp=='.') dotcount++;
  401.     else if (!isdigit(*sp)) return false;
  402.         sp++;
  403.      }
  404.      return fields<0 ? dotcount&1 : dotcount==fields;
  405. }
  406.  
  407.  
  408.  
  409. #ifdef KEEPTEST
  410.  
  411. char const cmdid[] ="keeptest";
  412.  
  413.     int
  414. main(argc, argv)
  415. int  argc; char  *argv[];
  416. {
  417.         while (*(++argv)) {
  418.         workname = *argv;
  419.         getoldkeys((RILE*)0);
  420.                 VOID printf("%s:  revision: %s, date: %s, author: %s, state: %s\n",
  421.                 *argv, prevrev.string, prevdate.string, prevauthor.string, prevstate.string);
  422.     }
  423.     exitmain(EXIT_SUCCESS);
  424. }
  425. #endif
  426.